﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MQ.Common;
using RabbitMQ.Client;

namespace MQ.Producer
{
    public static class Send_Normal
    {
        public static void Go()
        {
            string queueName = "normal";
            //1.创建连接
            var conn = RabbitMQHelper.GetConnection();
            {
                //2.创建信道
                var channel = conn.CreateModel();
                {
                    //创建队列
                    channel.QueueDeclare(queueName, false, false, false, null);
                    int i = 0;
                    while (true)
                    {
                        string msg = $"我是消息 '{++i}' ";
                        var body = Encoding.UTF8.GetBytes(msg);
                        //发送消息到rabbitmq,不指定交换机，会使用默认的交换机路由，默认的路由键和队列名称一致
                        Console.WriteLine($"发送消息 '{i}' 成功!");
                        channel.BasicPublish("", queueName, null, body);
                        Thread.Sleep(2000);
                    }
                }
            }
        }
    }
}
